﻿/// <reference path="framework.js" />

RexToy.registerType('fixtureString');
fixtureString.extend({
    $constructor: function () { },

    testTrimStart: function () {
        var a = "   abc ";
        var r = a.trimStart();
        $assert.areEqual("abc ", r, "trim start");
    },

    testTrimEnd: function () {
        var a = "   abc ";
        var r = a.trimEnd();
        $assert.areEqual("   abc", r, "trim end");
    },

    testTrim: function () {
        var a = "   abc ";
        var r = a.trim();
        $assert.areEqual("abc", r, "trim");
    },

    testStartWith: function () {
        var s = "abcdef";
        var r = "abd";
        var t = "abc";

        $assert.isTrue(s.startWith(t), "start with");
        $assert.isFalse(s.startWith(r), "start with");
    },

    testEndWith: function () {
        var s = "abcdef";
        var r = "cef";
        var t = "def";

        $assert.isTrue(s.endWith(t), "end with");
        $assert.isFalse(s.endWith(r), "end with");
    },

    testNullEmpty: function () {
        var s;
        var t = null;
        var r = '';

        $assert.isTrue(String.isNullOrEmpty(s), "null or empty");
        $assert.isTrue(String.isNullOrEmpty(t), "null or empty");
        $assert.isTrue(String.isNullOrEmpty(r), "null or empty");
        $assert.isTrue(String.isNullOrEmpty(String.Empty), "null or empty");
    },

    testFormat1: function () {
        var t = "{0}/{1}";
        var s = String.format(t, "1", "2");
        $assert.areEqual("1/2", s, "format");
    },

    testFormat2: function () {
        var t = "{0}/{1}/{0}";
        var s = String.format(t, "1", "2");
        $assert.areEqual("1/2/1", s, "format");
    }
});

RexToy.UnitTest.registerFixture("fixtureString");

RexToy.registerType('fixtureArray');
fixtureArray.extend({
    testContains1: function () {
        var arr = [1, 2, 3, 4, 5];
        $assert.isTrue(arr.contains(3));
        $assert.isFalse(arr.contains('3'));
    },

    testContains2: function () {
        var arr = ['1', '2', '3'];
        var item1 = '2';
        $assert.isTrue(arr.contains(item1));
    },

    testContains3: function () {
        var item1 = { name: 'a', age: 3 };
        var item2 = { name: 'b', age: 4 };
        var arr = [item1, item2];

        $assert.isTrue(arr.contains(item1));
    },

    testRemove1: function () {
        var arr = ['1', '2', '3'];
        arr.remove('2');
        $assert.areEqual(2, arr.length);
        $assert.isFalse(arr.contains('2'));
    },

    testRemove2: function () {
        var arr = ['1', '2', '3'];
        var r = arr.remove('4');
        $assert.isFalse(r);
        $assert.areEqual(3, arr.length);
    },

    testClear: function () {
        var arr = ['1', '2', '3'];
        arr.clear();
        $assert.areEqual(0, arr.length);
    },

    testIndexOf: function () {
        var arr = ['1', '2', '3'];
        var idx = arr.indexOf('1');
        $assert.areEqual(0, idx);
    },

    testForeach: function () {
        var item1 = { name: 'a', age: 3 };
        var item2 = { name: 'b', age: 4 };
        var arr = [item1, item2];

        arr.forEach(function (item, idx) {
            item.age += idx;
        });

        $assert.areEqual(3, arr[0].age);
        $assert.areEqual(5, arr[1].age);
    },

    testFirst: function () {
        var arr = ['a', 'b', 'c'];
        $assert.areEqual('a', arr.first());
    },

    testFirstNull: function () {
        var arr = [];
        $assert.isNull(arr.first());
    },

    testLast: function () {
        var arr = ['a', 'b', 'c'];
        $assert.areEqual('c', arr.last());
    },

    testLastNull: function () {
        var arr = [];
        $assert.isNull(arr.last());
    },

    testInsertAt: function () {
        var arr = ['3', '5'];
        arr.insertAt('4', 1);

        $assert.areEqual('3', arr[0]);
        $assert.areEqual('4', arr[1]);
        $assert.areEqual('5', arr[2]);
        $assert.areEqual(3, arr.length);
    },

    testRemoveAt: function () {
        var arr = ['3', '4', '5'];
        arr.removeAt(1);

        $assert.areEqual('3', arr[0]);        
        $assert.areEqual('5', arr[1]);
        $assert.areEqual(2, arr.length);
    }
});
RexToy.UnitTest.registerFixture("fixtureArray");

RexToy.registerType('fixtureNumber');
fixtureNumber.extend({
    testToPadString1: function () {
        var n = 15;
        $assert.areEqual('0015', n.toPadString(4));
    },

    testToPadString2: function () {
        var n = 15;
        $assert.areEqual('--15', n.toPadString(4, '-'));
    },

    testToPadString3: function () {
        var n = 15;
        $assert.areEqual('000f', n.toPadString(4, '0', 16));
    }
});
RexToy.UnitTest.registerFixture("fixtureNumber");

////////////Class use for test//////////////////
RexToy.registerInterface('IExecute');
IExecute.declare({
    preExecute: RexToy.AbstractFunction,
    execute: RexToy.AbstractFunction
});

RexToy.registerType('B');
B.extend({
    properties: {
        count: 0
    },

    hello: function () {
        return 'b';
    },

    test: function () {
        return 3;
    }
});

RexToy.registerType('D', B, [IExecute]);
D.extend({
    test: function () {
        return this.$super() + 1;
    },

    run: function () {
        return this.getCount();
    },
    
    execute: function () {
    },
    
    preExecute: function () {
    }
});

RexToy.registerType('Person');
Person.extend({
    properties: {
        name: String.Empty,
        age: 0,
        contacts: []
    },

    $constructor: function (name, age) {
        this.__name = name;
        this.__age = age;
    },

    setAge: function (val) {
        if (val < 0)
            throw 'age < 0';
        this.__age = val;
    }
});

RexToy.registerType('Employee', Person);
Employee.extend({
    properties: {
        salary: 0,
        level: 6,
        tasks: []
    },

    $constructor: function (name, age, salary) {
        this.__name = name;
        this.__age = age;
        this.__salary = salary;
    },

    appendTask: function (t) {
        this.__tasks.push(t);
    }
});

RexToy.registerType('Manager', Employee);
Manager.extend({
    properties: {
        level: 3
    }
});

////////////Class use for test End//////////////////

RexToy.registerType('fixtureTypeSystem');
fixtureTypeSystem.extend({
    testInherit: function () {
        var d = new D();
        $assert.areEqual('b', d.hello());
    },

    testCallbase: function () {
        var d = new D();
        $assert.areEqual(4, d.test());
    },

    testAutoProperty1: function () {
        var p = new Person();
        p.setName('a');
        p.setAge(1);

        $assert.areEqual(1, p.getAge());
        $assert.areEqual('a', p.getName());
    },

    testAutoProperty2: function () {
        try {
            var p = new Person('z', 50);
            p.setAge(-1);
            $assert.fail('should throw exception.');
        } catch (e) {
            $assert.areEqual('z', p.getName());
            $assert.areEqual(50, p.getAge(), 'throw while set age = -1, it should keep 50');
        }
    },

    testAutoProperty3: function () {
        var e = new Employee();
        var m = new Manager();

        $assert.areEqual(6, e.getLevel());
        $assert.areEqual(3, m.getLevel());
    },

    testPropertyInheritAndConstructorInit: function () {
        var e1 = new Employee('b', 5, 1000);
        var e2 = new Employee('c', 10, 2000);

        $assert.areEqual('b', e1.getName());
        $assert.areEqual(5, e1.getAge());
        $assert.areEqual(1000, e1.getSalary());

        $assert.areEqual('c', e2.getName());
        $assert.areEqual(10, e2.getAge());
        $assert.areEqual(2000, e2.getSalary());
    },

    testRefArray: function () {
        var e1 = new Employee('b');
        e1.appendTask('A');
        var e2 = new Employee('c');
        e2.appendTask('B');
        e2.appendTask('C');

        $assert.areEqual('b', e1.getName());
        $assert.areEqual(1, e1.getTasks().length);
        $assert.areEqual('A', e1.getTasks()[0]);
        $assert.areEqual('c', e2.getName());
        $assert.areEqual(2, e2.getTasks().length);
        $assert.areEqual('B', e2.getTasks()[0]);
        $assert.areEqual('C', e2.getTasks()[1]);
    },

    testArrayInherit: function () {
        var e = new Employee();
        $assert.areEqual(0, e.getTasks().length);
        $assert.areEqual(0, e.getContacts().length);

        var m = new Manager();
        $assert.areEqual(0, m.getContacts().length);
    },

    testTypename: function () {
        var e = new Employee();
        $assert.areEqual('Employee', e.getType());
        var m = new Manager();
        $assert.areEqual('Manager', m.getType());
    }
});
RexToy.UnitTest.registerFixture("fixtureTypeSystem");

RexToy.registerType('fixtureStringBuilder');
fixtureStringBuilder.extend({
    testInit: function () {
        var sb = new RexToy.Text.StringBuilder();
        $assert.areEqual(String.Empty, sb.toString());
    },

    testAppend: function () {
        var sb = new RexToy.Text.StringBuilder();
        sb.append('123');
        sb.appendFormat('{0}{1}', 'a', 'bc');
        $assert.areEqual('123abc', sb.toString());
    },

    testClear: function () {
        var sb = new RexToy.Text.StringBuilder();
        sb.append('123');
        sb.clear();
        $assert.areEqual(String.Empty, sb.toString());
    }
});
RexToy.UnitTest.registerFixture("fixtureStringBuilder");

